/*
 * 前端查询数据类汇总
 * @author: huangzong
 */
import C from '@/common/common.js'

class Guest {
  /*
   * 打开日志报表
   */
  async openSettlement () {
    return await C.openBrowserWindow('/settlement', {}, {
      transparent: true,
      frame: false,
      width: 1200,
      height: 700,
      useSpa: false,
      spaModel: 'settlement'
    })
  }

  /*
   * 获取固定时间的收入和支出
   * 格式: return {
   *  allIn: '',
   *  allOut: '',
   *  inList: [],
   *  outList: []
   * }
   */
  async getPayInOut (filter) {
    let data = await C.ajax('/shop/settlement/getPayInOut', filter)
    let rdata = {
      allIn: 0,
      allOut: 0,
      inList: [],
      outList: []
    }
    if (data.length) {
      data.forEach(row => {
        if (row.type == 1) {
          rdata.allIn += row.money
          rdata.inList.push(row)
        }
        if (row.type == 2) {
          rdata.allOut += row.money
          rdata.outList.push(row)
        }
      })
    }
    rdata.allIn = Number(rdata.allIn).toFixed(2)
    rdata.allOut = Number(rdata.allOut).toFixed(2)
    return rdata
  }

  /*
   * 获取业绩和提成信息
   */
  async getYejiTicheng (filter) {
    let data = await C.ajax('/shop/settlement/getYejiTicheng', filter)
    data.allyj = (data.allxy + data.allky).toFixed(2)
    data.alltc = (data.allxt + data.allkt + data.allst + data.allqt + data.allxj + data.allkj).toFixed(2)
    return data
  }

  /*
   * 获取客户的消费信息（从日志中）
   */
  async getGuestxiaofei (filter) {
    let data = await C.ajax('/shop/settlement/getGuestxiaofei', filter)
    let redata = {
      allBalance: 0,
      allGiftBalance: 0,
      balanceList: [],
      giftBalanceList: []
    }
    if (data.length) {
      data.forEach(row => {
        redata.allBalance += row.balance
        redata.allGiftBalance += row.giftbalance
        if (row.balance != 0) redata.balanceList = this.sort(row, 'balance', redata.balanceList)
        if (row.giftbalance != 0) redata.giftBalanceList = this.sort(row, 'giftbalance', redata.giftBalanceList)
      })
    }
    return redata
  }

  /*
   * 获取客户的消费信息（从订单中）
   */
  async getGuestOrderXiaofei (filter) {
    let data = await C.ajax('/shop/settlement/getGuestOrderXiaofei', filter)
    // 整合数据
    let newData = {
      allxj: 0,
      allkh: 0,
      allye: 0,
      allzs: 0,
      xjList: [],
      khList: [],
      yeList: [],
      zsList: []
    }
    if (data.length) {
      data.forEach(row => {
        newData.allxj += row.recemoney
        newData.allkh += row.cardreduce
        newData.allye += row.balance
        newData.allzs += row.giftbalance
        if (row.recemoney != 0) newData.xjList = this.sort(row, 'recemoney', newData.xjList)
        if (row.cardreduce != 0) newData.khList = this.sort(row, 'cardreduce', newData.khList)
        if (row.balance != 0) newData.yeList = this.sort(row, 'balance', newData.yeList)
        if (row.giftbalance != 0) newData.zsList = this.sort(row, 'giftbalance', newData.zsList)
      })
    }
    return newData
  }

  sort (row, value, array) {
    for (var i = 0; i < array.length; i++) {
      if (row[value] > array[i][value]) {
        for (var k = array.length - 1; k >= i; k--) {
          array[k + 1] = array[k]
        }
        array[i] = row
        return array
      }
    }
    array.push(row)
    return array
  }
}

export default new Guest()
